﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace TRAC_Demo
{
    static class CurrentUser
    {
        // T.R.A.C.
        // Main Author: Ryan Redburn
        // Date: 10/4/2013

        // This class is used to assist in applying user preferences as well as determining account management access
        // It also supplies the value for the Creating Agent in ticket creation

        #region [ Fields ]

        // All fields in this class represent info obtained from a user on account creation

        // Private fields for internal class use

        private static string _userID;
        private static int _empID;
        private static string _empFirstName;
        private static string _empLastName;
        private static string _privileges;
        private static Color _userColor;

        // Public properties that can be used to retrieve values from the class but not set them

        public static string UserID
        {
            get { return _userID; }
        }

        public static int EmpID
        {
            get { return _empID; }
        }

        public static string EmpFirstName
        {
            get { return _empFirstName; }
        }

        public static string EmpLastName
        {
            get { return _empLastName; }
        }

        public static string Privileges
        {
            get { return _privileges; }
        }

        public static Color UserColor
        {
            get { return _userColor; }
        }

        #endregion

        #region [ Methods ]

        // Sets all relevant information pertaining to the current user
        public static void Intialization(string userID)
        {
            _userID = userID;
            GetAcctInfo(userID);
            GetEmpInfo(_empID);
        }

        #endregion

        #region [ Helper Methods ]

        // Retrieves required information from the database
        // First and last name
        private static void GetEmpInfo(int empID)
        {
            TRACobjectsDataContext TRAC_Agents = new TRACobjectsDataContext();

            // Lambda function that finds the current user by employee id
            var agent = TRAC_Agents.Agents.Where(x => x.EmployeeID == empID).Single();

            _empFirstName = agent.fName.Trim();
            _empLastName = agent.lName.Trim();

            TRAC_Agents.Dispose();
        }

        // Retrieves required information from the xml doc
        // Employee ID, account privilege level, and user selected color
        private static void GetAcctInfo(string userID)
        {
            List<string> acctInfo = AccountKeeper.GetAcctInfo(userID);
            List<int> acctColor = ColorKeeper.RetrieveColor(acctInfo[4]);

            _empID = int.Parse(acctInfo[1]);
            _privileges = acctInfo[3];
            _userColor = Color.FromArgb(acctColor[0], acctColor[1], acctColor[2], acctColor[3]);
        }

        #endregion
    }
}
